<?php

namespace App\Console\Commands;

use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;

class AutoCompleteOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order.auto_complete';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '已签收订单72效时后自动确认订单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try{
            DB::table('order')
            ->where('order_type',Order::TYPE_ONLINE)
            ->where('send_type',Order::POST)
            ->where('status',Order::STATUS_WAIT_COLLECT)
            ->where('send_time','<',Carbon::now()->subDays(3)->toDateTimeString())
            ->whereNotNull('express_order')
            ->select('id','online_order','express_name','express_order')
            ->orderBy('id','asc')
            ->chunk(10,function($collections){
                dispatch(new \App\Jobs\AutoCompleteOrder($collections));
                if(App::environment('local')){
                    \Illuminate\Support\Facades\Log::channel('express_dispatch')->info('插入队列成功', ['data' => $collections]);
                }
            });
        }catch (\Exception $exception){
            \Illuminate\Support\Facades\Log::channel('express_dispatch')->info($exception->getMessage());
        }
    }
}
